dojo.provide("dojot.album.PicSort");
dojo.require("dojo.dnd.Source");
dojo.requireLocalization("dojot", "common");
dojo.requireLocalization("dojot", "album");
dojo.declare("dojot.album.PicSort",null,{
	
	//拖放的容器区域
	source:null,
	saveSortOrderUrl:"",
	albumPicJsonListUrl:"groupAlbumPicJsonList",
	albumId:-1,
	constructor: function(node,params){
		dojo.mixin(this, dojo.mixin({}, params));
		//国际化信息
		var messages1= dojo.i18n.getLocalization("dojot", "common",dojo.locale);
		var messages2= dojo.i18n.getLocalization("dojot", "album",dojo.locale);
		this._messages = dojo.mixin({},messages1,messages2);
		// replace the avatar string to make it more human readable
		//替换Avator的一个方法，以便修改我们的在拖动的时候提示文字。
		var that = this;
		dojo.dnd.Avatar.prototype._generateText = function(){
			return (this.manager.copy ? that._messages.copy : that._messages.move) + this.manager.nodes.length + that._messages.item;
		};
		var param = {
			creator : dojo.hitch(this,"_addPicItem")
		};
		this.source = new dojo.dnd.Source(node,param);
		//不让我们的图片可以被copy
		this.source.copyState = function(){
			return false;
		}
		this._getPicItems();		
	},	
	_getPicItems: function() {
		//生成图片列表
		var that = this;
		dojo.xhrGet({
			url: that.albumPicJsonListUrl , 
			content:{
				albumId:that.albumId
			},
			load: function(data){
				that._createPicItems(data);
			},
			handleAs: "json"
		});
	},
	_createPicItems: function(data){
		//生成图片
		this.source.selectAll().deleteSelectedNodes();
		this.source.insertNodes(false, data);
	},
	_addPicItem: function(item,hint){
		//添加单个图片item
		var type = [];
		var node = dojo.doc.createElement("dl");
		dojo.addClass(node,"albumPhoto");
		var fileName = item.small;	
		if(hint == "avatar"){
			node = dojo.doc.createElement("span");
			node.innerHTML = this._messages.moveCorrectStation;
		}else{
			var html = [];												
			html.push('<dt class="thumb"><a href="#"><img src="'+fileName+'" width="90" alt="" style="width:1em"/></a></dt>');
			node.innerHTML = html.join(""); 
			node.id = dojo.dnd.getUniqueId();
			node.picId = item.picId;
		}
		return {node: node, data: item, type: type};
	},
	saveSort: function(ids){
		//保存图片排序顺序
		var that = this;
		dojo.xhrPost({
			url:that.saveSortOrderUrl, 
			content: {
				albumId: that.albumId,
				ids: ids
			},
			load:function(responseDate){
				if(responseDate){
					alert(that._messages.saveSuccess);
				}else{
					alert(that._messages.saveFaiture);
				}
			},
			handleAs: "json"
		})
	},
	editSort: function(){
		var ids = [];	
		dojo.query('.dojoDndItem ','albumPhotoListSmall').forEach(function(item){
			ids.push(item.picId);
		});
		this.saveSort(ids.join(","));
	}
});